home *** CD-ROM | disk | FTP | other *** search
Text File | 1998-04-30 | 8.9 KB | 411 lines | [TEXT/MPS ] |
- /*
- File: OTTcpCatchSample.cp
-
- Contains: TCP catch sample
-
- Copyright: © 1993-1997 by Apple Computer, Inc., all rights reserved.
-
- */
-
-
- // OT TCP Catch Test Program (as an SIOW Tool)
-
- #define lint
- #include <QuickDraw.h>
- #include <stdio.h>
- #include <StdLib.h>
- #include <TextUtils.h>
- #include <strings.h>
- #include <String.h>
- #include <Events.h>
- #include <Menus.h>
- #include <Devices.h>
- #include <OpenTransport.h>
- #include <OpenTptInternet.h>
-
-
- /*******************************************************************************
- ** GLOBAL VARIABLES
- ********************************************************************************/
-
- #define kMaxDataLen 256
-
- const size_t kMyPoolSize = 60000;
-
- InetPort gCatchPort = 0;
- InetHost gCatchIpAddr = 0;
- InetPort gPitchPort = 0;
- InetHost gPitchIpAddr = 0;
-
- unsigned short gBindCompleted = 0;
- unsigned short gDataToRead = 0;
- unsigned short gCallRcvOrdDiscon = 0;
-
- OTEventCode gCode;
- TEndpoint* gCookie;
- OSStatus gErr;
-
- unsigned char data[kMaxDataLen];
-
- struct InetAddress reqsin, retsin, rcvsin;
-
- /*******************************************************************************
- ** Function Prototypes
- ********************************************************************************/
-
- void Inits(void);
- void CleanUp(void);
- void Idle(void);
- void DoIt(void);
- Boolean UserAbort(void);
-
-
- /*******************************************************************************
- ** main function
- ********************************************************************************/
-
- void main()
- {
- Inits();
- DoIt();
- CleanUp();
- }
-
- /*******************************************************************************
- ** Initialize Quickdraw and ASLM
- ********************************************************************************/
-
- void Inits()
- {
- InitGraf(&qd.thePort);
- if (InitOpenTransport() != kOTNoError)
- {
- fprintf(stderr, "OTTcpCatch: Could not initialize ASLM, exiting\n");
- exit(1);
- }
- }
-
- /*******************************************************************************
- ** Clean up at the end
- ********************************************************************************/
-
- void CleanUp()
- {
- CloseOpenTransport();
- }
-
- /*******************************************************************************
- ** Idle
- ********************************************************************************/
-
- void Idle()
- {
- SystemTask();
- }
-
- /*******************************************************************************
- ** EventHandler
- ********************************************************************************/
-
- pascal void EventHandler(void*, OTEventCode event, OTResult result, void* cookie)
- {
- OTEventCode tempevent = 0;
-
- switch(event)
- {
- case T_OPENCOMPLETE:
- gErr = (OSStatus)result;
- gCookie = (TEndpoint*) cookie;
- gCode = event;
- break;
- case T_DATA:
- gDataToRead = 1;
- break;
- case T_BINDCOMPLETE:
- gBindCompleted = 1;
- break;
- case T_ORDREL:
- gCallRcvOrdDiscon = 1;
- break;
- default:
- // OTDebugBreak("EventHandler got unexpected event");
- tempevent = event;
- break;
- }
- return;
- }
-
- /*******************************************************************************
- ** DoIt
- ********************************************************************************/
-
- void DoIt()
- {
- TEndpoint* ep = NULL;
- TEndpointInfo info;
- TBind req, ret;
- TCall mycall;
- OSStatus err = kOTNoError;
- long myport;
- InetHost myaddr;
- char mystr[255];
- OTFlags flags;
- long bytes = 0;
-
- myport = 0;
- fprintf(stderr, "Catch port ? (enter TCP port number)\n");
- if ( gets(mystr) != 0 )
- {
- stringtonum(mystr, &myport);
- gCatchPort = (InetPort) myport;
- }
- myaddr = 0;
- fprintf(stderr, "Catch IP address ? (enter IP address)\n");
- if ( gets(mystr) != 0 )
- {
- if ( OTInetStringToHost(mystr, &myaddr) == 0 )
- {
- gCatchIpAddr = (InetHost) myaddr;
- }
- }
- myport = 0;
- fprintf(stderr, "Pitch port ? (enter TCP port number)\n");
- if ( gets(mystr) != 0 )
- {
- stringtonum(mystr, &myport);
- gPitchPort =(InetPort) myport;
- }
- myaddr = 0;
- fprintf(stderr, "Pitch IP address ? (enter IP address)\n");
- if ( gets(mystr) != 0 )
- {
- if ( OTInetStringToHost(mystr, &myaddr) == 0 )
- {
- gPitchIpAddr = (InetHost) myaddr;
- }
- }
- fprintf(stderr, "The program will listen for a packet on port <%d>\n", gCatchPort);
- fprintf(stderr, "until \'cmd .\' keys are pressed\n");
-
- memset(&reqsin, 0, sizeof(struct InetAddress));
- memset(&retsin, 0, sizeof(struct InetAddress));
- memset(&rcvsin, 0, sizeof(struct InetAddress));
- memset(&req, 0, sizeof(TBind));
- memset(&ret, 0, sizeof(TBind));
-
- OTInitInetAddress(&reqsin, gCatchPort, gCatchIpAddr);
-
- do
- {
- //
- // Now create a TCP
- //
- #if 1
- gCode = 0;
- gCookie = NULL;
- gErr = 0;
- err = OTAsyncOpenEndpoint(OTCreateConfiguration(kTCPName), 0,
- &info, EventHandler, 0);
- if ( err == 0 )
- {
- while ( gCode == 0 )
- OTIdle();
- err = gErr;
- }
- if ( err != 0 )
- {
- ep = NULL;
- fprintf(stderr,"ERROR: OpenEndpoint(\"TCP\") failed with %d\n", err);
- break;
- }
- else
- {
- ep = gCookie;
- }
- #else
- ep = OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, &info, &err);
-
- if ( ep == NULL || err != kOTNoError )
- {
- ep = NULL;
- fprintf(stderr,"ERROR: OpenEndpoint(\"TCP\") failed with %d\n", err);
- break;
- }
-
- err = ep->SetSynchronous();
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: SetSynchronous() failed with %d\n", err);
- break;
- }
-
- //
- // Install notifier we're going to use for testing
- //
- err = ep->InstallNotifier(&EventHandler, 0);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: InstallNotifier() failed with %d\n", err);
- break;
- }
- #endif
- //
- // Try to bind
- //
- req.addr.len = sizeof(struct InetAddress);
- req.addr.buf = (unsigned char *) &reqsin;
- req.qlen = 5; // don't care for tcp
- ret.addr.maxlen = sizeof(struct InetAddress);
- ret.addr.buf = (unsigned char *) &retsin;
-
- // bind tcp to current address and port
- err = ep->Bind(&req, &ret);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: Bind() failed with %d\n", err);
- break;
- }
-
- err = ep->SetSynchronous();
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: SetSynchronous() failed with %d\n", err);
- break;
- }
-
- err = ep->SetBlocking();
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: SetBlocking() failed with %d\n", err);
- break;
- }
-
- memset(&mycall, 0, sizeof(TCall));
-
- mycall.addr.maxlen = sizeof(struct InetAddress);
- mycall.addr.len = sizeof(struct InetAddress);
- mycall.addr.buf = (unsigned char *) &rcvsin;
- mycall.opt.maxlen = 0;
- mycall.opt.buf = 0;
- mycall.udata.maxlen = 0;
-
- err = ep->Listen(&mycall);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: Listen() failed with %d\n", err);
- break;
- }
-
- ep->Accept(ep, &mycall);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: Accept() failed with %d\n", err);
- break;
- }
-
- err = ep->SetAsynchronous();
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: SetAsynchronous() failed with %d\n", err);
- break;
- }
-
- do
- {
- //
- // Try to receive some data
- //
- if ( gDataToRead == 1 )
- {
- gDataToRead = 0;
- bytes = ep->Rcv(data, kMaxDataLen, &flags);
- if (bytes >= 0)
- fprintf(stderr, "Packet received\nsize: <%d> data: %s", bytes, data);
- else
- fprintf(stderr, "ERROR: Rcv() failed with %d\n", bytes);
- break;
- }
- Idle();
- } while (!UserAbort());
- } while (false);
-
- if ( ep != NULL )
- {
-
- err = ep->SetSynchronous();
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: SetSynchronous() failed with %d\n", err);
- return;
- }
-
- err = ep->SndOrderlyDisconnect();
- if ( err != kOTNoError )
- {
- if (err == kOTLookErr)
- fprintf(stderr, "SndOrderlyDisconnect() returns %d\n", err);
- else
- fprintf(stderr, "ERROR: SndOrderlyDisconnect() failed with %d\n", err);
- }
-
- while ( gCallRcvOrdDiscon == 0 )
- Idle();
- err = ep->RcvOrderlyDisconnect();
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: RcvOrderlyDisconnect() failed with %d\n", err);
- }
-
- //
- // Remove notifier
- //
- ep->RemoveNotifier();
- //
- // Try to Unbind
- //
- /*
- err = ep->Unbind();
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: Unbind() returned %d\n", err);
- }
- */
- //
- // Get rid of endpoint.
- //
- err = OTCloseProvider(ep);
- if ( err != kOTNoError )
- {
- fprintf(stderr, "ERROR: CloseEndpoint() failed with %d\n", err);
- }
- }
- fprintf(stderr, "Bye\n");
- }
-
- /*******************************************************************************
- ** IsPressed
- ********************************************************************************/
-
- Boolean IsPressed(UInt16 k, KeyMap map)
- {
- UInt8* keyMap = (UInt8*)map;
- return (keyMap[k >> 3] >> (k & 7) & 1);
- }
-
- /*******************************************************************************
- ** CmdKey
- ********************************************************************************/
-
- Boolean UserAbort()
- {
- KeyMap keyMap;
-
- GetKeys(keyMap);
- if ( IsPressed(0x37, keyMap) &&
- (IsPressed(0x2F, keyMap) || IsPressed(0x41, keyMap)) )
- return true;
- else
- return false;
- }
-
-